﻿$j.extend({
    autoCompleteUrl: function(settings) {/*parameters is arrary*/
        var return_url;
        var _url = (settings.url || '' == settings.url) ? $j('form:first').attr('action') : settings.url;
        if ('' == settings.ajaxMethodName)
            return_url = _url;
        else {
            return_url = (_url.indexOf('?') == -1) ? (_url + '?_state=ajaxmethod') : (url + '&_state=ajaxmethod');
            return_url += "&methodname=" + encodeURIComponent(settings.ajaxMethodName) + "&returntype=" + settings.returnType || 4;
        }
        if (settings.parameters) {
            var len = settings.parameters.length;
            if (len != 0) {
                for (var i = 0; i < len; i++)
                    return_url += "&methodparameter" + i + "=" + encodeURIComponent(settings.parameters);
            }
        }
        return return_url;
    }
});

$j.fn.extend({
    autoCompleteOnce: function(settings, autoSettings) {
        var KEY = 'data';
        var obj = this;
        var id = '#' + $j(obj).attr('id');
        $j(id).data('settings', settings);
        $j(id).data('autoSettings', autoSettings);
        $j(obj).bind("focus", function() {
            if ($j(id).data(KEY) == undefined) {
                if (typeof (settings) == 'object') {
                    $Page.Invoke(settings.ajaxMethodName,
                     { url: settings.url, ReturnType: settings.returnType || 4, Success: su },
                     settings.parameters);
                }
                else {
                    $j.post(settings, su);
                }
            }
        });
        function su(js) {
            var data = eval(js);
            $j(id).data(KEY, data);
            $j(id).autocomplete($(id).data(KEY), autoSettings);
        }
    }
});